package com.zdd.jdkproxy;

import com.zdd.steady.IPerson;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @version 1.0
 * @Author: carl
 * @Description: jdk动态代理类
 * @Date: Create in 18:21 2020/10/7 0007
 */
public class JdkMeipo implements InvocationHandler {

    private IPerson target;

    public IPerson getInstance(IPerson target){
        this.target = target;
        Class<?> clazz = target.getClass();
        return (IPerson) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(this.target,args);
        after();
        return result;
    }

    private void before(){
        System.out.println("我是媒婆，已经收到你的需求，开始物色");
    }

    private void after(){
        System.out.println("双方同意，开始交往");
    }

    public static void main(String[] args){
        JdkMeipo jdkMeipo = new JdkMeipo();
        IPerson zhaoliu = jdkMeipo.getInstance(new ZhaoLiu());
        zhaoliu.findLove();
    }
}
